﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.ComponentModel;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class TFUNCTIONS 
    {

        public TFUNCTIONS()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_FUNCTIONS();
            DataContext = MV;
            InitializeComponent();
        }
      
        private void New_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {
                if (dwMaster == null)
                    return;
                DataRow dwData = TBMain.NewRow();
                ((VICC_FUNCTIONS)MV).SetUpNewRow(ref dwData);
                LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "FNSEQ", 1);
                dwData["ROWNID"] = LFD; 
                Insert(dwData);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Insert(DataRow _dwFUNC)
        {
            TBMain.Rows.InsertAt(_dwFUNC, 0);
            vwMaster.View.MoveCurrentToFirst();
            ((VICC_FUNCTIONS)MV).DeleteICC_FUNCTIONS(_dwFUNC, 1);
            ((VICC_FUNCTIONS)MV).InsertICC_FUNCTIONS(_dwFUNC, app.company);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            SResult = ((VICC_FUNCTIONS)MV).QueryICC_FUNCTIONS(dwData, 9999);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "FNSEQ", 1);
            dwData["ROWNID"] = LFD;
            Insert(dwData); 
            base.CopyCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_FUNCTIONS)MV).UpdateICC_FUNCTIONS(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_FUNCTIONS)MV).DeleteICC_FUNCTIONS(dwMaster, 1);

            string filter = "ROWNID='" + dwMaster["ROWNID"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }  
  
        private void txtFunctionType_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (dwMaster == null)
                    return;
                string tag = txtType.Text.Trim();
                dwMaster["NAMESPACE"] = "WPFExcelReport";
                dwMaster["TYPE"] = tag;
                dwMaster["FULLNAME"] = tag;
                dwMaster["FILENAME"] = tag + ".dll";
            }
        }
     
    }
}
